package saucaca.fragment;

import java.util.ArrayList;
import java.util.List;

import lemon.kenhnhacvn.BaseActivity;
import lemon.kenhnhacvn.BaseEngine;
import lemon.kenhnhacvn.MainActivity;
import lemon.kenhnhacvn.R;

import org.json.JSONException;

import saucaca.Adapter.TopicAdapter;
import saucaca.CustomView.CustomGridview;
import saucaca.CustomView.FilteredArrayAdapter;
import saucaca.JSON.JsonReader;
import saucaca.JSON.WServiceLib;
import saucaca.Object.AlbumObject;
import saucaca.Object.AreaObject;
import saucaca.Object.HomeData;
import saucaca.Object.SearchObject;
import saucaca.Object.SingerObject;
import saucaca.Object.SongObject;
import saucaca.Object.TopicObject;
import saucaca.Util.TagName;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class OnlineSongFragment extends Fragment {
	private CustomGridview gridVietNam, gridQuocte;
	List<TopicObject> listCate;
	List<AreaObject> listAreas;
	private MainActivity mActivity = null;
	String area = "";
	String cateCode = "";

	@SuppressLint("NewApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//		mActivity.getActionBar().hide();
		View view = inflater.inflate(R.layout.song_online, container, false);
		findViewById(view);
		if(HomeData.homeListArea!=null&&HomeData.homeListCate!=null){
			listAreas=new ArrayList<AreaObject>();
			for (int i = 0; i < HomeData.homeListArea.size(); i++) {
				listAreas.add( HomeData.homeListArea.get(i));
			}
			for (int i = 0; i < listAreas.size(); i++) {
				if (listAreas.get(i).getAREA_CODE().equals("VN")) {
					listAreas.remove(i);
				}
			}
			listCate=new ArrayList<TopicObject>();
			for (int i = 0; i < HomeData.homeListCate.size(); i++) {
				String SHOW="1";
				if(Character.toString(HomeData.homeListCate.get(i).getIS_BTYPE().charAt(0)).equals(SHOW)){
					listCate.add(HomeData.homeListCate.get(i));
				}
			}
			gridVietNam.setAdapter(new TopicAdapter(mActivity, listCate));
			gridQuocte.setAdapter(new AreaAdapter(mActivity, listAreas));
		}
		mActivity.myActionBar.setMenu(R.string.string_baihat, false, true);
		return view;
	}
	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		mActivity.myActionBar.setMenu(R.string.string_baihat, false, true);
	}

	public void findViewById(View view) {
		gridVietNam = (CustomGridview) view.findViewById(R.id.song_CustomGridview_Vietnam);
		gridVietNam.setOnItemClickListener(clickListenerVietnam);
		gridVietNam.setExpanded(true);
		gridQuocte = (CustomGridview) view.findViewById(R.id.song_CustomGridview_Quocte);
		gridQuocte.setOnItemClickListener(clickListenerQuocte);
		gridQuocte.setExpanded(true);
	}

	OnItemClickListener clickListenerVietnam = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			String data = "VN" + TagName.keyBundle + listCate.get(position).getCATE_CODE() + TagName.keyBundle + listCate.get(position).getTXNAME();
			mActivity.switchContent(OnlineSongFragmentList.class.getName(), data);
		}
	};
	OnItemClickListener clickListenerQuocte = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			AreaObject areaObject = listAreas.get(position);
			String data = areaObject.getAREA_CODE() + TagName.keyBundle + "" + TagName.keyBundle + areaObject.getTXNAME();
			mActivity.switchContent(OnlineSongFragmentList.class.getName(), data);
		}
	};

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof MainActivity) {
			mActivity = (MainActivity) activity;
		}
	}

	public class AreaAdapter extends ArrayAdapter<AreaObject> {
		Context context;
		List<AreaObject> data = new ArrayList<AreaObject>();
		ImageLoader imageLoader = ImageLoader.getInstance();
		DisplayImageOptions options;

		public AreaAdapter(Context context, List<AreaObject> data) {
			super(context, R.layout.item_topic_gridview, data);
			this.context = context;
			this.data = data;

			options = new DisplayImageOptions.Builder().showImageOnFail(R.drawable.ic_default).showStubImage(R.drawable.ic_default).showImageForEmptyUri(R.drawable.ic_default).cacheInMemory().cacheOnDisc().build();

		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View row = convertView;
			RecordHolder holder = null;
			if (row == null) {
				LayoutInflater inflater = ((Activity) context).getLayoutInflater();
				row = inflater.inflate(R.layout.item_topic_gridview, parent, false);
				holder = new RecordHolder();
				holder.txtTitle = (TextView) row.findViewById(R.id.custom_view_album_artist);
				holder.imageItem = (ImageView) row.findViewById(R.id.custom_view_album_image);
				holder.imageItem.setLayoutParams(BaseActivity.linear_params_3);
				row.setTag(holder);
			} else {
				holder = (RecordHolder) row.getTag();
			}
			AreaObject item = data.get(position);
			holder.txtTitle.setText(item.getTXNAME());
			imageLoader.displayImage("", holder.imageItem, options);
			if (item.getAREA_CODE() == "VN") {
				return null;
			} else
				return row;
		}

		class RecordHolder {
			TextView txtTitle;
			ImageView imageItem;
		}
	}
}
